package com.crealth.db;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.log4j.Logger;

import com.crealth.ConstantDefine;
import com.crealth.manager.ClienteleInfoManager;
import com.crealth.manager.MybatisManager;
import com.crealth.memcached.UserCached;
import com.crealth.model.CompanyModel;
import com.crealth.model.UserModel;
import com.crealth.utils.StringUtil;


public class UserDb {
	private static Logger errorLog = Logger.getLogger("error_log");
	
	public static UserModel selectUserByLoginName(String loginName) {
		try {
			UserModel userModel = MybatisManager.userMapper.selectUserByLoginName(loginName);
			return userModel;
		}
		catch (PersistenceException persistenceException) {
			try {
				MybatisManager.initSessionFactory();
				UserModel userModel = MybatisManager.userMapper.selectUserByLoginName(loginName);
				return userModel;
			} catch (IOException e) {
				errorLog.error("selectUserByLoginName", e);
			}
			return null;
		}
		catch (Exception e) {
			errorLog.error("selectUserByLoginName", e);
			return null;
		}		
	}
	
	public static void updateUserByUserID(UserModel userModel) {
		MybatisManager.userMapper.updateUserByUserID(userModel);
		MybatisManager.commit();
	}
	
	public static UserModel selectUserByName(String userName) {
		UserModel userModel = MybatisManager.userMapper.selectUserByName(userName);
		
		return userModel;
	}
    
    public static List<UserModel> selectUnderling(int userID) {
    	UserModel user = UserCached.selectUserByID(userID);
    	Integer userPos = user.getPosition();
    	List<UserModel> userLs = null;
    	if (ConstantDefine.Team_Manager == userPos) {
    		int teamID = user.getTeamID();
    		userLs = MybatisManager.userMapper.selectTeamUserByTeamID(teamID);
    	}
    	else if (ConstantDefine.Group_Leader == userPos) {
    		userLs = MybatisManager.userMapper.selectGroupUsers(user.getUserName());
    	}
    	else if (ConstantDefine.Branch_Mgr == userPos || ConstantDefine.Branch_Mgr_Fu == userPos
    			|| userPos == ConstantDefine.Customer_Service ) {
    		int companyID = user.getCompanyID();
    		CompanyModel belongCom = CompanyDb.getCompanyByID(companyID);
    		if (belongCom != null) {
    			companyID = belongCom.getId();
    		}
    		List<Integer> comIDls = CompanyDb.selectDepartmentIDByID(companyID);
    		if (comIDls != null) {
    			comIDls.add(companyID);
    		}
    		int topCompanyID = CompanyDb.getTopCompanyID(companyID);
    		String strIDs = StringUtil.mergeListIntegerToString(comIDls, ",");
    		userLs = selectCompanyAndDepartmentUser(strIDs, topCompanyID);
    	}
    	
    	return userLs;
    }
    
    public static List<UserModel> selectBranchUser(int companyID) {
    	List<UserModel> userLs = MybatisManager.userMapper.selectBranchUser(companyID);
    	
    	return userLs;
    }
    
    public static List<UserModel> selectCompanyAndDepartmentUser(String companyIDs, int topCompanyID) {
    	List<UserModel> userLs = MybatisManager.userMapper.selectCompanyAndDepartmentUser(companyIDs, topCompanyID);
    	
    	return userLs;
    }
    
    public static List<Integer> selectBranchUserIDs(int companyID) {
    	List<Integer> ids = MybatisManager.userMapper.selectBranchUserIDs(companyID);
    	
    	return ids;
    }
    
    public static UserModel selectUserByID(int userID) {
    	UserModel user = MybatisManager.userMapper.selectUserByID(userID);
    	
    	return user;
    }
    
    public static List<Integer> selectUnderlingIDs(int userID) {
    	UserModel user = UserCached.selectUserByID(userID);
    	Integer userPos = user.getPosition();
    	List<Integer> ids = new ArrayList<Integer>();
    	if (ConstantDefine.Team_Manager == userPos) {
    		ids = MybatisManager.userMapper.selectUnderlingIDs(userID);
    	} else if (ConstantDefine.Branch_Mgr == userPos || ConstantDefine.Branch_Mgr_Fu == userPos
    			|| userPos == ConstantDefine.Customer_Service) {
    		int companyID = user.getCompanyID();
    		CompanyModel belongCom = CompanyDb.getCompanyByID(companyID);
    		if (belongCom != null) {
    			companyID = belongCom.getId();
    			ids = MybatisManager.userMapper.selectBranchUserIDs(companyID);
    		}
    	} else if (ConstantDefine.InstallDpmMGR == userPos) {
    		ids = MybatisManager.userMapper.selectUserIDsByPosition(ConstantDefine.Installmenter);
    	}
    
    	return ids;
    }    
    public static List<Integer> selectAllSalesManIDs() {
    	List<Integer> ids = MybatisManager.userMapper.selectAllSalesManIDs();
        
    	return ids;
    }
    public static List<Integer> selectAllSalesManAndTeamMgrIDs() {
    	List<Integer> ids = MybatisManager.userMapper.selectAllSalesManAndTeamMgrIDs();
        
    	return ids;
    }
    public static List<Integer> selectAllOnjobSalesManIDs() {
    	List<Integer> ids = MybatisManager.userMapper.selectAllOnjobSalesManIDs();
        
    	return ids;
    }
    
    public static List<UserModel> selectTeamUserByTeamID(int teamID) {
    	List<UserModel> userls = MybatisManager.userMapper.selectTeamUserByTeamID(teamID);
    	
    	return userls;
    }
    
    public static List<UserModel> selectCompanyUsersByPosition(int pos) {    	
    	List<UserModel> userls = MybatisManager.userMapper.selectCompanyUsersByPosition(pos);
    	
    	return userls;
    }
    
    public static List<Integer> selectUserIDsByPosition(int pos) {
    	List<Integer> idls = MybatisManager.userMapper.selectUserIDsByPosition(pos);
    	
    	return idls;
    }
    
    public static List<Integer> selectAdminIDs() {
    	List<Integer> ids = MybatisManager.userMapper.selectAdminIDs();
    	
    	return ids;
    }
    
    public static Integer selectUserPosition(int userID) {
    	Integer pos = MybatisManager.userMapper.selectUserPosition(userID);
    	
    	return pos;
    }
    
    public static Integer selectLeaderID(int userID) {
    	Integer leaserID = MybatisManager.userMapper.selectLeaderID(userID);
    	
    	return leaserID;
    }
    
    public static List<UserModel> selectAllUser() {
    	List<UserModel> userLs = MybatisManager.userMapper.selectAllUser();
    	
    	return userLs;
    }
    
    public static List<UserModel> selectCompanyPositionUser(int companyID, int posi) {
    	List<UserModel> userLs = MybatisManager.userMapper.selectCompanyPositionUser(companyID, posi);
    	
    	return userLs;
    }
    
    public static List<UserModel> getBranchCompanyMgr(int companyID) {
    	List<UserModel> userLs = selectCompanyPositionUser(companyID, ConstantDefine.Branch_Mgr);
    	List<UserModel> companyMgrFuls = selectCompanyPositionUser(companyID, ConstantDefine.Branch_Mgr_Fu);
    	if (companyMgrFuls != null && companyMgrFuls.size() > 0) {
    		for (UserModel curUser : companyMgrFuls ) {
    			userLs.add(curUser);
    		}
    	}
    	
    	return userLs;
    }
    
    public static List<UserModel> selectCompanyTopManager() {
    	List<UserModel> userLs = MybatisManager.userMapper.selectCompanyTopManager();
        
    	return userLs;
    }
    
    public static List<UserModel> selectAllOnJobUser() {
    	List<UserModel> userLs = MybatisManager.userMapper.selectAllOnJobUser();
    	
    	return userLs;
    }
	
	public static void insertStaff(UserModel userModel) {
		try {
			MybatisManager.userMapper.insertStaff(userModel);
			MybatisManager.commit();
		}
		catch (Exception e) {
			errorLog.error("insertStaff error", e);
		}
	}
	
	private static void updateUserOnline(int userID, int setOnline) {
		MybatisManager.userMapper.updateUserOnline(userID, setOnline);
		MybatisManager.commit();
	}
	
	public static void updateUserOnlineStatus(int userID, boolean isOnline) {
		int setOnline = 0;
		if (isOnline) {
			setOnline = 1;
		}		
		updateUserOnline(userID, setOnline);
	}
	
	public static List<UserModel> selectSalesManager() {
		List<UserModel> leaders = MybatisManager.userMapper.selectSalesManager();
	
		return leaders;
	}
	
	public static List<UserModel> selectGroupUsers(String groupMgr) {
		List<UserModel> users = MybatisManager.userMapper.selectGroupUsers(groupMgr);
		
		return users;
	}
	
	public static void cleanOnlineUser() {
		MybatisManager.userMapper.cleanOnlineUser();
		MybatisManager.commit();
	}
	
	public static Integer selectMarkClientPreID(int userID) {
		int preID = MybatisManager.userMapper.selectMarkClientPreID(userID);
	
		return preID;
	}
    // 更新  客户表的ID
    public static void updateMarkClientPreID(int userID, int setPreID) {
    	MybatisManager.userMapper.updateMarkClientPreID(userID, setPreID);
    	MybatisManager.commit();
    }
    
    public static void updateUserLoginPwd(int userID, String setNewPwd) {
    	MybatisManager.userMapper.updateUserLoginPwd(userID, setNewPwd);
    	MybatisManager.commit();
    }
    
    public static void updateSipAccountByID(int userID, 
    		String sipServerIP, String sipAct, String sipPwd,
    		String incomingServerIP, String incomingSipAct, String incomingSipPwd) {
    	MybatisManager.userMapper.updateSipAccountByID(userID, sipServerIP, sipAct, sipPwd, 
    			incomingServerIP, incomingSipAct, incomingSipPwd);
    	MybatisManager.commit();
    	UserCached.deleUserCachedByID(userID);
    }
	
    public static Integer updateUserMOBByID(int userID, int setMOB) {
    	Integer res = MybatisManager.userMapper.updateUserMOBByID(userID, setMOB);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static Integer updateTeamUserLeaderByTeamID(int teamID, int leaderID) {
    	Integer res = MybatisManager.userMapper.updateTeamUserLeaderByTeamID(teamID, leaderID);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static Integer updateTeamLeaderByID(int userID, int leaderID) {
    	Integer res = MybatisManager.userMapper.updateTeamLeaderByID(userID, leaderID);
    	MybatisManager.commit();
    	
    	return res;
    }	
    
	public static String selectClientVersion() {
		String version = MybatisManager.userMapper.selectClientVersion();
		
		return version;
	}
	
	public static Integer selectIsShowConsumerInfo() {
		Integer res = MybatisManager.userMapper.selectIsShowConsumerInfo();
		
		return res;
	}
	
	public static Integer updateIsShowConsumerInfo(int setValue) {
		Integer res = MybatisManager.userMapper.updateIsShowConsumerInfo(setValue);
		MybatisManager.commit();
		
		return res;
	}
	
	public static Integer selectGroupCallType() {
		Integer res = MybatisManager.userMapper.selectGroupCallType();
		
		return res;
	}
    public static Integer updateGroupCallType(int companyID, int setValue) {
    	Integer res = MybatisManager.userMapper.updateGroupCallType(setValue);
    	MybatisManager.commit();
    	if (res != null) {
    		Boolean groupCall = true;
    		if (setValue == 0) {
    			groupCall = false;
    		}
    		ClienteleInfoManager.groupCallTypeMap.put(companyID, groupCall);
    	}
		
		return res;
    }
    
    public static Timestamp selectUploadGroupCallTm() {
    	Timestamp tm = MybatisManager.userMapper.selectUploadGroupCallTm();
    	
    	return tm;
    }
    
    public static Integer updateUserMacAddr(int userID, String macAddr) {
    	Integer res = MybatisManager.userMapper.updateUserMacAddr(userID, macAddr);
    	MybatisManager.commit();
    	
    	return res;
    }
    public static Integer selectSipDispatchType() {
    	Integer res = MybatisManager.userMapper.selectSipDispatchType();
    	
    	return res;
    }    
    public static Integer updateSipDispatchType(int setValue) {
    	Integer res = MybatisManager.userMapper.updateSipDispatchType(setValue);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static Integer updateUserLastLoginMacAddr(int userID, String macAddr) {
    	Integer res = MybatisManager.userMapper.updateUserLastLoginMacAddr(userID, macAddr);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static Integer updateUserLoginMacAddrToMac(int userID) {
    	Integer res = MybatisManager.userMapper.updateUserLoginMacAddrToMac(userID);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static Integer updateEntryFile(int userID, String setEntryFile, String setEntryFileUrl) {
    	Integer res = MybatisManager.userMapper.updateEntryFile(userID, setEntryFile, setEntryFileUrl);
    	MybatisManager.commit();
    	
    	return res;
    }
    public static Integer updatePictureFile(int userID, String setPictureFile, String setPictureFileUrl) {
    	Integer res = MybatisManager.userMapper.updatePictureFile(userID, setPictureFile, setPictureFileUrl);
    	MybatisManager.commit();
    	
    	return res;
    }
    public static Integer updateIdentityFile(int userID, String setIdentityFile, String setIdentityFileUrl) {
    	Integer res = MybatisManager.userMapper.updateIdentityFile(userID, setIdentityFile, setIdentityFileUrl);
    	MybatisManager.commit();
    	
    	return res;
    }
    public static Integer updateBankFile(int userID, String setBankFile, String setBankFileUrl) {
    	Integer res = MybatisManager.userMapper.updateBankFile(userID, setBankFile, setBankFileUrl);
    	MybatisManager.commit();
    	
    	return res;
    }
    public static Integer updateOtherFile(int userID, String setOtherFile, String setOtherFileUrl) {
    	Integer res = MybatisManager.userMapper.updateOtherFile(userID, setOtherFile, setOtherFileUrl);
    	MybatisManager.commit();
    	
    	return res;
    }
    
    public static Integer updateCompanyIDByTeamID(int teamID, int companyID) {
    	Integer res = MybatisManager.userMapper.updateCompanyIDByTeamID(teamID, companyID);
    	MybatisManager.commit();
    	
    	return res;
    }
    
	
}
